package cn.gzmli.dtt.common.utils.msgpush;

import cn.gzmli.dtt.common.config.cache.Config;
import cn.gzmli.dtt.common.entity.Message;
import cn.gzmli.dtt.common.utils.SpringContextUtil;
import cn.gzmli.dtt.common.utils.msgpush.bean.Push;
import cn.gzmli.dtt.common.utils.msgpush.bean.PushCallBack;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class FlowNotifPush {

	public  static void sendMessage(Integer senderID, String curFlowNodeID, String formID, int eventType, Integer tStatus, String tlStatus, PushCallBack pushCallBack)throws Exception{

		List<Map<String,Object>> msgs= getRecivers(curFlowNodeID,formID,eventType,tStatus,tlStatus);
		if(msgs!=null){
			for(Map<String,Object> msg:msgs){
				
				Push push=new Push();
				
				//消息推送前执行回调函数事前方法
				if(pushCallBack!=null){
					pushCallBack.beginPush(msgs,msg,push);
				}

				push.setTitle(msg.get("message").toString());
				push.setType(msg.get("flowNodeID").toString());
				push.setFormID(msg.get("formID").toString());
				push.setLogID(Integer.parseInt(msg.get("taskLogID").toString()));
				push.settStatus(msg.get("tStatus")==null?0:Integer.parseInt(msg.get("tStatus").toString()));
				
				Message message=new Message();
				message.setTitle(push.getTitle());
				message.setLogID(Integer.parseInt(msg.get("taskLogID").toString()));
				message.setMessageType(push.getType());
				message.setSenderID(senderID);
				message.setContent(msg.get("content")==null?null:msg.get("content").toString());
				Integer reciverID=Integer.parseInt(msg.get("processerID").toString());
				//AndroidPushService.push(push,reciverID.toString());
				//IosPushService.apnpush(push,reciverID.toString());
				Config.pushMsgToCache(reciverID, message);

			}
		}

	}

	private static List<Map<String,Object>>  getRecivers(String curFlowNodeID,String formID,int eventType,Integer tStatus,String tlStatus)throws Exception{

		String eventCond="";
		switch(eventType){
			case 0:
				eventCond="rm.reciveState=1";
				break;

			case 1:
				eventCond="rm.dealState=1";
				break;

			case 2:
				eventCond="rm.sendState=1";
				break;

		}
		List<Map<String,Object>> result=new ArrayList<Map<String,Object>>();
		String sql=String.format("SELECT MAX(tl.id) taskLogID ,"
						+ "tl.processerID processerID,"
						+ "rm.message message,"
						+ "rm.tStatus tStatus,"
						+ "tl.formID formID,"
						+ "tl.flowNodeID flowNodeID "
						+ "FROM flownode_message rm,TaskLog tl "
						+ " WHERE rm.curFlowNodeID=tl.flowNodeID AND  tl.formID=%s "
						+ "AND rm.flowNodeID='%s' AND %s AND rm.status=1 AND %s %s "
                        + "AND IF (rm.tlStatus IS NOT NULL AND rm.tlStatus<>'',FIND_IN_SET(tl.status,rm.tlStatus),1) <> 0 "
						+ "GROUP BY tl.flowNodeID,tl.processerID",
				formID,
				curFlowNodeID,
				eventCond,
				tStatus==null?"rm.tstatus IS NULL":"rm.tstatus="+tStatus,
				tlStatus!=null?" AND tl.status IN ("+tlStatus+") ":" "
		);
		JdbcTemplate jdbcTemplate=(JdbcTemplate) SpringContextUtil.getBean("jdbcTemplate");
		result=jdbcTemplate.queryForList(sql);
		return result;
	}

	
}
